package org.ftc.web;

import org.ftc.domain.Users;
import org.ftc.service.FtcLogService;
import org.ftc.service.FtcLogService.Log;
import org.ftc.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller 
public class PersonalController {
	
	@Autowired
	private UsersService usersService;
	@Autowired
	private FtcLogService logService;
	
	/*@RequestMapping(value = "/personal.htm")
    public String personal( ModelMap model ) {
		model.addAttribute("personal", usersService.securityByUser());
		return "personal";
    }
	
	@RequestMapping(value = "/personal/update.json", method=RequestMethod.POST)
    public @ResponseBody String personal(Users users, ModelMap model ) {
		Boolean s = true;
		logService.before();
		Users entity = usersService.securityByUser();
		entity.setGender(users.getGender());
		entity.setName(users.getName());
		entity.setEmail(users.getEmail());
		s = usersService.update(entity);
		logService.save(Log.UPDATE, users.getUserName(), this.getClass().toString() ,ToStringBuilder.reflectionToString(entity) );
		return s.toString(); 
    }*/
	
	@RequestMapping(value = "/personal/password.htm")
    public String password( ModelMap model ) {
		model.addAttribute("personal", usersService.securityByUser());
		return "personal/password";
    }
	
	@RequestMapping(value = "/personal/password/update.json", method=RequestMethod.POST)
    public @ResponseBody boolean password(Users users, ModelMap model ) {
		Boolean s = true;
		logService.before();
		Users entity = usersService.securityByUser();
		entity.setPassword( users.getPassword() );
		s = usersService.update(entity);
		logService.save(Log.PASSWORD, users.getUserName(), this.getClass().toString() );
		return s;
    } 
	
}
